package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingPlanVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class NursingLevelServiceImpl implements NursingLevelService {
    private final NursingLevelMapper nursingLevelMapper;
    /**
     * 查询所有护理等级
     */
    @Override
    public List<NursingLevelVo> findNursingLevelList() {
        List<NursingLevel> list=nursingLevelMapper.findNursingLevelList();
        return BeanUtil.copyToList(list,NursingLevelVo.class);
    }

    /**
     * 新增护理等级
     */
    @Override
    public void insertNursingLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        nursingLevelMapper.insertNursingLevel(nursingLevel);
    }

    /**
     * 分页查询
     */
    @Override
    @Transactional
    public PageResponse<NursingLevelVo> queryNursingLevel(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingLevel> nursingLevel = nursingLevelMapper.queryNursingLevel(name, status);
        // 判断条件查询的结果是否为空
        if(CollectionUtil.isEmpty(nursingLevel.getResult())){
            return PageResponse.of(nursingLevel,NursingLevelVo.class);
        }
        // 得到每个护理等级ids
        List<Long> ids = nursingLevel.getResult().stream().map(item -> item.getId()).collect(Collectors.toList());
        // 根据护理等级id查询每一个护理等级被引用的次数
        List<Map<String, Object>> maps = nursingLevelMapper.queryNursingLevelByids(ids);
        //  如果没有任何一个护理项目被引用, 直接返回
        if(CollectionUtil.isEmpty(maps)){
            for (NursingLevel level : nursingLevel) {
                level.setCount(0);
            }
            return PageResponse.of(nursingLevel, NursingLevelVo.class);
        }
        //将护理计划引用数量集合转成Map集合 , 方便后期获取护理计划引用次数 Map<护理目id,关联数量>
        Map<Integer, Long> collect = maps.stream().collect(Collectors.toMap(map -> Integer.valueOf(map.get("planId").toString()), map -> Long.valueOf(map.get("count").toString())));
        // 将实体类转化为VO
        List<NursingLevelVo> nursingLevelVoList = nursingLevel.getResult().stream().map(item -> {
            NursingLevelVo bean = BeanUtil.toBean(item, NursingLevelVo.class);
            //获取护理项目对应的引用数量
            Long orDefault = collect.getOrDefault(item.getId().intValue(), 0L);
            bean.setCount(orDefault.intValue());
            return bean;
        }).collect(Collectors.toList());
        return PageResponse.<NursingLevelVo>builder()
                .page(nursingLevel.getPageNum())
                .pageSize(nursingLevel.getPageSize())
                .pages(Long.valueOf(nursingLevel.getPages()))
                .total(nursingLevel.getTotal())
                .records(nursingLevelVoList)
                .build();
    }

    /**
     * 根据id查询
     */
    @Override
    public NursingLevelVo findNursingLevelById(Integer id) {
        NursingLevel nursingLevel=nursingLevelMapper.findNursingLevelById(id);
        return BeanUtil.toBean(nursingLevel,NursingLevelVo.class);
    }

    /**
     * 修改护理等级
     */
    @Override
    @Transactional
    public void modifyNursingLevel(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        Long id = nursingLevel.getId();
        List<Map<String, Object>> maps = nursingLevelMapper.queryNursingLevelByids(CollectionUtil.toList(id));
        // 被引用就不能删除、修改、启用/禁用
        if(!CollectionUtil.isEmpty(maps)){
            throw new BaseException(BasicEnum.DATA_REFERENCED);
        }
        nursingLevelMapper.modifyNursingLevel(nursingLevel);
    }

    /**
     * 删除护理等级
     */
    @Override
    @Transactional
    public void deleteNursingLevel(Integer id) {
        List<Map<String, Object>> maps = nursingLevelMapper.queryNursingLevelByids(CollectionUtil.toList(Long.valueOf(id)));
        // 被引用就不能删除、修改、启用/禁用
        if(!CollectionUtil.isEmpty(maps)){
            throw new BaseException(BasicEnum.DATA_REFERENCED);
        }
        nursingLevelMapper.deleteNursingLevel(id);
    }

    /**
     * 启用/禁用
     */
    @Override
    @Transactional
    public void modifyNursingLevelStatus(Integer id, Integer status) {
        List<Map<String, Object>> maps = nursingLevelMapper.queryNursingLevelByids(CollectionUtil.toList(Long.valueOf(id)));
        // 被引用就不能删除、修改、启用/禁用
        if(!CollectionUtil.isEmpty(maps)){
            throw new BaseException(BasicEnum.DATA_REFERENCED);
        }
        nursingLevelMapper.modifyNursingLevelStatus(id,status);
    }
}
